package com.example.playbox

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.playbox.bean.Game
import com.example.playbox.utils.MySQLiteOpenHelper

class UpdateGamesActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var etGameid: EditText
    private lateinit var etGamename: EditText
    private lateinit var etGametime: EditText
    private lateinit var etGamenote: EditText
    private lateinit var cbReserved: CheckBox
    private lateinit var btnSearch: Button
    private lateinit var btnEdit: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_update_games)
        initView()
    }

    private fun initView() {
        etGameid = findViewById(R.id.et_gameid)
        btnSearch = findViewById(R.id.btn_search)
        etGamename = findViewById(R.id.et_gamename)
        etGametime = findViewById(R.id.et_gametime)
        etGamenote = findViewById(R.id.et_gamenote)
        cbReserved = findViewById(R.id.cb_reserved)
        btnEdit = findViewById(R.id.btn_edit)

        btnSearch.setOnClickListener(this)
        btnEdit.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_search -> searchOrder()
            R.id.btn_edit -> updateOrder()
        }
    }

    private fun searchOrder() {
        val gameid = etGameid.text.toString().trim()
        val dao = MySQLiteOpenHelper(applicationContext)
        dao.open()
        val game = dao.getGames(gameid)
        etGamename.setText(game.gameName)
        etGametime.setText(game.gameTime)
        etGamenote.setText(game.gameNote)
        cbReserved.isChecked = game.reserved
        dao.close()
    }

    private fun updateOrder() {
        val gameid = etGameid.text.toString().trim()
        val gamename = etGamename.text.toString().trim()
        val gametime = etGametime.text.toString().trim()
        val gamenote = etGamenote.text.toString().trim()
        val reserved = cbReserved.isChecked
        val username = getCurrentUsername()

        val dao = MySQLiteOpenHelper(applicationContext)
        dao.open()
        val game = dao.getGames(gameid)

        if (game.userName != username) {
            Toast.makeText(this, "您没有权限修改此游戏信息", Toast.LENGTH_SHORT).show()
            dao.close()
            return
        }

        val updatedGame = Game(gameid, gamename, gametime, gamenote, username, reserved)
        val result = dao.updateGames(updatedGame)
        if (result > 0) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show()
        }
        dao.close()
    }

    private fun getCurrentUsername(): String {
        val sp = getSharedPreferences("loginInfo", MODE_PRIVATE)
        return sp.getString("loginUserName", "") ?: ""
    }
}